package com.fulihui.wgj.admin.api.mass;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.mass.request.MassCancelRequest;
import com.fulihui.wgj.admin.api.mass.request.MassLogPageQueryRequest;
import com.fulihui.wgj.admin.api.mass.request.SendMassRequest;
import com.fulihui.wgj.admin.api.mass.vo.MassMsgLogVO;
import com.fulihui.wgj.admin.api.qrcode.vo.QrcodeLabelInfoVO;
import com.fulihui.wgj.admin.integration.ParamQrcodeServiceClient;
import com.fulihui.wgj.admin.integration.SendWxMassServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-08-01 13:55
 */
@RestController
@RequestMapping("/mass")
@Api(tags = "图文群发相关接口")
public class MassMsgController extends BaseController {

    @Autowired
    private ParamQrcodeServiceClient paramQrcodeServiceClient;

    @Autowired
    private SendWxMassServiceClient sendWxMassServiceClient;

    @PostMapping("/qrcode/taglist")
    @ApiOperation("获取渠道二维码标签列表")
    public Result<List<QrcodeLabelInfoVO>> taglist(HttpServletRequest request) {
        return paramQrcodeServiceClient.queryQrcodeLabel(getAppid(request));
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ApiOperation("发送图文群发消息")
    public Result<Boolean> send(@RequestBody @Valid SendMassRequest request, BindingResult validResult,
                                HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendWxMassServiceClient.sendMass(request);
    }

    @RequestMapping(value = "/count", method = RequestMethod.POST)
    @ApiOperation("计算人数")
    public Result<String> countSendUser(@RequestBody @Valid SendMassRequest request, BindingResult validResult,
                                        HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        return sendWxMassServiceClient.countUser(request);
    }

    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ApiOperation("取消发送")
    public Result<Boolean> cancel(@RequestBody @Valid MassCancelRequest request, BindingResult validResult) {
        Result<Boolean> result = sendWxMassServiceClient.cancelMass(request);
        return result;
    }

    @RequestMapping(value = "/log", method = RequestMethod.POST)
    @ApiOperation("发送图文群发消息记录")
    public Result<PageView<MassMsgLogVO>> sendLog(@RequestBody @Valid MassLogPageQueryRequest request, BindingResult validResult,
                                                  HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<MassMsgLogVO> pageView = sendWxMassServiceClient.massLog(request);
        return JsonResultBuilder.succ(pageView);
    }

}
